<?php

use app\Request;
use crmeb\services\UploadService;
use Fastknife\Service\BlockPuzzleCaptchaService;
use Fastknife\Service\ClickWordCaptchaService;
use crmeb\services\SystemConfigService;
use think\exception\ValidateException;
use think\facade\Config;
use think\facade\Log;

if (!function_exists('get_tree_value')) {
    /**
     * 获取
     * @param array $data
     * @param int|string $value
     * @return array
     */
    function get_tree_value(array $data, $value, array &$childrenValue = [])
    {
        foreach ($data as &$item) {
            if ($item['value'] == $value) {
                $childrenValue[] = $item['value'];
                if ($item['pid']) {
                    $value = $item['pid'];
                    unset($item);
                    return get_tree_value($data, $value, $childrenValue);
                }
            }
        }
        return $childrenValue;
    }
}

if (!function_exists('is_brokerage_statu')) {

    /**
     * 是否能成为推广人
     * @param float $price
     * @return bool
     */
    function is_brokerage_statu(float $price)
    {
        if (!sys_config('brokerage_func_status')) {
            return false;
        }
        $storeBrokerageStatus = sys_config('store_brokerage_statu', 1);
        if ($storeBrokerageStatus == 1) {
            return false;
        } else if ($storeBrokerageStatus == 2) {
            return false;
        } else {
            $storeBrokeragePrice = sys_config('store_brokerage_price', 0);
            return $price >= $storeBrokeragePrice;
        }
    }
}





if (!function_exists('time_tran')) {
    /**
     * 时间戳人性化转化
     * @param $time
     * @return string
     */
    function time_tran($time)
    {
        $t = time() - $time;
        $f = array(
            '31536000' => '年',
            '2592000'  => '个月',
            '604800'   => '星期',
            '86400'    => '天',
            '3600'     => '小时',
            '60'       => '分钟',
            '1'        => '秒'
        );
        foreach ($f as $k => $v) {
            if (0 != $c = floor($t / (int)$k)) {
                return $c . $v . '前';
            }
        }
    }
}

if (!function_exists('url_to_path')) {
    /**
     * url转换路径
     * @param $url
     * @return string
     */
    function url_to_path($url)
    {
        $path = trim(str_replace('/', DS, $url), DS);
        if (0 !== strripos($path, 'public'))
            $path = 'public' . DS . $path;
        return app()->getRootPath() . $path;
    }
}

if (!function_exists('path_to_url')) {
    /**
     * 路径转url路径
     * @param $path
     * @return string
     */
    function path_to_url($path)
    {
        return trim(str_replace(DS, '/', $path), '.');
    }
}

if (!function_exists('get_image_thumb')) {
    /**
     * 获取缩略图
     * @param $filePath
     * @param string $type all|big|mid|small
     * @param bool $is_remote_down
     * @return mixed|string|string[]
     */
    function get_image_thumb($filePath, string $type = 'all', bool $is_remote_down = false)
    {
		if (!sys_config('image_thumb_status')) return $filePath;
        if (!$filePath || !is_string($filePath) || strpos($filePath, '?') !== false) return $filePath;
        try {
			$arr = explode('.', $filePath);
			$ext_name = trim($arr[count($arr) - 1]);
			if (!in_array($ext_name, ['png', 'jpg', 'jpeg'])) {
				return $filePath;
			}
            $upload = UploadService::getOssInit($filePath, $is_remote_down);
            $data   = $upload->thumb('', $type);
            $image  = $type == 'all' ? $data : $data[$type] ?? $filePath;
        } catch (\Throwable $e) {
            $image = $filePath;
            //            throw new ValidateException($e->getMessage());
            \think\facade\Log::error('获取缩略图失败，原因：' . $e->getMessage() . '----' . $e->getFile() . '----' . $e->getLine() . '----' . $filePath);
        }
        $data = parse_url($image);
        if (!isset($data['host']) && (substr($image, 0, 2) == './' || substr($image, 0, 1) == '/')) {//不是完整地址
            $image = sys_config('site_url') . $image;
        }
        //请求是https 图片是http 需要改变图片地址
        if (strpos(request()->domain(), 'https:') !== false && strpos($image, 'https:') === false) {
            $image = str_replace('http:', 'https:', $image);
        }
        return $image;
    }
}

if (!function_exists('get_thumb_water')) {
    /**
     * 处理数组获取缩略图、水印
     * @param $list
     * @param string $type
     * @param array|string[] $field 1、['image','images'] type 取值参数:type 2、['small'=>'image','mid'=>'images'] type 取field数组的key
     * @param bool $is_remote_down
     * @return array|mixed|string|string[]
     */
    function get_thumb_water($list, string $type = 'small', array $field = ['image'], bool $is_remote_down = false)
    {
        if (!$list || !$field) return $list;
        $baseType = $type;
        $data     = $list;
        if (is_string($list)) {
            $field = [$type => 'image'];
            $data  = ['image' => $list];
        }
        if (is_array($data)) {
            foreach ($field as $type => $key) {
                if (is_integer($type)) {//索引数组，默认type
                    $type = $baseType;
                }
                //一维数组
                if (isset($data[$key])) {
                    if (is_array($data[$key])) {
                        $path_data = [];
                        foreach ($data[$key] as $k => $path) {
                            $path_data[] = get_image_thumb($path, $type, $is_remote_down);
                        }
                        $data[$key] = $path_data;
                    } else {
                        $data[$key] = get_image_thumb($data[$key], $type, $is_remote_down);
                    }
                } else {
                    foreach ($data as &$item) {
                        if (!isset($item[$key]))
                            continue;
                        if (is_array($item[$key])) {
                            $path_data = [];
                            foreach ($item[$key] as $k => $path) {
                                $path_data[] = get_image_thumb($path, $type, $is_remote_down);
                            }
                            $item[$key] = $path_data;
                        } else {
                            $item[$key] = get_image_thumb($item[$key], $type, $is_remote_down);
                        }
                    }
                }
            }
        }
        return is_string($list) ? ($data['image'] ?? '') : $data;
    }
}

if (!function_exists('put_image')) {
    /**
     * 获取图片转为base64
     * @param string $avatar
     * @return bool|string
     */
    function put_image($url, $filename = '')
    {

        if ($url == '') {
            return false;
        }
        try {
            if ($filename == '') {

                $ext = pathinfo($url);
                if ($ext['extension'] != "jpg" && $ext['extension'] != "png" && $ext['extension'] != "jpeg") {
                    return false;
                }
                $filename = time() . "." . $ext['extension'];
            }

			$pathArr = parse_url($url);
			$path = $pathArr['path'] ?? '';
			if ($path && file_exists(public_path() . trim($path, '/'))) {
				return $path;
			} else {
				//文件保存路径
				ob_start();
				$url = str_replace('phar://', '', $url);
				readfile($url);
				$img = ob_get_contents();
				ob_end_clean();
				$path = 'uploads/qrcode';
				$fp2 = fopen(public_path() . $path . '/' . $filename, 'a');
				fwrite($fp2, $img);
				fclose($fp2);
				return $path . '/' . $filename;
			}
        } catch (\Exception $e) {
            return false;
        }
    }
}

if (!function_exists('make_path')) {

    /**
     * 上传路径转化,默认路径
     * @param $path
     * @param int $type
     * @param bool $force
     * @return string
     */
    function make_path($path, int $type = 2, bool $force = false)
    {
        $path = DS . ltrim(rtrim($path));
        switch ($type) {
            case 1:
                $path .= DS . date('Y');
                break;
            case 2:
                $path .= DS . date('Y') . DS . date('m');
                break;
            case 3:
                $path .= DS . date('Y') . DS . date('m') . DS . date('d');
                break;
        }
        try {
            if (is_dir(app()->getRootPath() . 'public' . DS . 'uploads' . $path) == true || mkdir(app()->getRootPath() . 'public' . DS . 'uploads' . $path, 0777, true) == true) {
                return trim(str_replace(DS, '/', $path), '.');
            } else return '';
        } catch (\Exception $e) {
            if ($force)
                throw new \Exception($e->getMessage());
            return '无法创建文件夹，请检查您的上传目录权限：' . app()->getRootPath() . 'public' . DS . 'uploads' . DS . 'attach' . DS;
        }

    }
}

if (!function_exists('check_phone')) {
    /**
     * 手机号验证
     * @param $phone
     * @return false|int
     */
    function check_phone($phone)
    {
        return preg_match("/^1[3456789]\d{9}$/", $phone);
    }
}

if (!function_exists('check_mail')) {
    /**
     * 邮箱验证
     * @param $mail
     * @return false|int
     */
    function check_mail($mail)
    {
        if (filter_var($mail, FILTER_VALIDATE_EMAIL)) {
           return true;
        } else {
           return false;
        }
    }
}

if (!function_exists('aj_captcha_check_one')) {
    /**
     * 验证滑块1次验证
     * @param string $token
     * @param string $pointJson
     * @return bool
     */
    function aj_captcha_check_one(string $captchaType, string $token, string $pointJson)
    {
        aj_get_serevice($captchaType)->check($token, $pointJson);
        return true;
    }
}

if (!function_exists('aj_captcha_check_two')) {
    /**
     * 验证滑块2次验证
     * @param string $token
     * @param string $pointJson
     * @return bool
     */
    function aj_captcha_check_two(string $captchaType, string $captchaVerification )
    {
        aj_get_serevice($captchaType)->verificationByEncryptCode($captchaVerification);
        return true;
    }
}

if (!function_exists('aj_captcha_create')) {
    /**
     * 创建验证码
     * @return array
     */
    function aj_captcha_create(string $captchaType)
    {
        return aj_get_serevice($captchaType)->get();
    }
}

if (!function_exists('aj_get_serevice')) {

    /**
     * @param string $captchaType
     * @return ClickWordCaptchaService|BlockPuzzleCaptchaService
     */
    function aj_get_serevice(string $captchaType)
    {
        $config = Config::get('ajcaptcha');
        switch ($captchaType) {
            case "clickWord":
                $service = new ClickWordCaptchaService($config);
                break;
            case "blockPuzzle":
                $service = new BlockPuzzleCaptchaService($config);
                break;
            default:
                throw new ValidateException('captchaType参数不正确！');
        }
        return $service;
    }
}

if (!function_exists('mb_substr_str')) {

    /**
     * 截取制定长度,并使用填充
     * @param string $value
     * @param int $length
     * @param string $str
     * @return string
     * @author 等风来
     * @email 136327134@qq.com
     * @date 2022/12/1
     */
    function mb_substr_str(string $value, int $length, string $str = '...', int $type = 0)
    {
        if (mb_strlen($value) > $length) {
            $value = mb_substr($value, 0, $length - mb_strlen($str)) . $str;
        }

        //等于1时去掉数组
        if ($type === 1) {
            $value = preg_replace('/[0-9]/', '', $value);
        }

        return $value;
    }
}
if (!function_exists('msectime')) {
	/**
	 * 毫秒时间戳
	 *
	 * @return float
	 */
	function msectime()
	{
		[$mSec, $sec] = explode(' ', microtime());

		return (float)sprintf('%.0f', (floatval($mSec) + floatval($sec)) * 1000);
	}
}

if (!function_exists('response_log_write')) {

	/**
	 * 日志写入
	 * @param array $data
	 * @param string $logType
	 * @param string $type
	 * @param $id
	 * @return void
	 */
	function response_log_write(array $data, string $logType = \think\Log::ERROR, string $type = '', int $id = 0)
	{
		$request = app()->make(Request::class);

		try {

			if (!$type || !$id) {
				foreach ([
							 'adminId' => 'admin',
							 'kefuId' => 'kefu',
							 'uid' => 'user',
							 'supplierId' => 'supplier',
							 'cashierId' => 'cashier',
							 'storeId' => 'store',
							 'outId' => 'out',
						 ] as $value => $vv) {
					if ($request->hasMacro($value)) {
						$id = $request->{$value}();
						$type = $vv;
					}
				}
			}

			//日志内容
			$log = [
				$id,//管理员ID
				$type,
				$request->ip(),//客户ip
				ceil(msectime() - ($request->time(true) * 1000)),//耗时（毫秒）
				$request->method(true),//请求类型
				str_replace("/", "", $request->rootUrl()),//应用
				$request->baseUrl(),//路由
				json_encode($request->param(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),//请求参数
				json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),//报错数据
			];

			Log::write(implode("|", $log), $logType);
		} catch (\Throwable $e) {

			$data = [
				'file' => $e->getFile(),
				'line' => $e->getLine(),
				'trace' => $e->getTrace(),
				'previous' => $e->getPrevious(),
			];
			Log::error(json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));

		}
	}
}

if (!function_exists('supplier_config')) {

	/**
	 * @param int $supplierId
	 * @param string $name
	 * @param null $default
	 * @return array|string|null
	 */
	function supplier_config(int $supplierId, string $name, $default = null)
	{
		if (empty($name)) {
			return $default;
		}
		/** @var SystemConfigService $configService */
		$configService = app('sysConfig');
		$configService->setSupplier($supplierId);
		$sysConfig = $configService->get($name);
		if (is_array($sysConfig)) {
			foreach ($sysConfig as &$item) {
				if (strpos($item, '/uploads/system/') !== false) {
					$item = set_file_url($item);
				}
			}
		} else {
			if (strpos($sysConfig, '/uploads/system/') !== false) {
				$sysConfig = set_file_url($sysConfig);
			}
		}
		$config = is_array($sysConfig) ? $sysConfig : trim($sysConfig);
		if ($config === '' || $config === false) {
			return $default;
		} else {
			return $config;
		}
	}
}

if (!function_exists('stringToIntArray')) {

	/**
	 * 处理ids等并过滤参数
	 * @param string $string
	 * @param string $separator
	 * @return array
	 */
	function stringToIntArray(string $string, string $separator = ',')
	{
		return !empty($string) ? array_unique(array_diff(array_map('intval', explode($separator, $string)), [0])) : [];
	}
}

if (!function_exists('getFileHeaders')) {

	/**
	 * 获取文件大小头部信息
	 * @param string $url
	 * @param $isData
	 * @return array
	 */
	function getFileHeaders(string $url, $isData = true)
	{
		stream_context_set_default(['ssl' => ['verify_peer' => false, 'verify_peer_name' => false]]);
		$header['size'] = 0;
		$header['type'] = 'image/jpeg';
		if (!$isData) {
			return $header;
		}
		try {
			$headerArray = get_headers(str_replace('\\', '/', $url), true);
			if (!isset($headerArray['Content-Length'])) {
				$header['size'] = 0;
			} else {
				if (is_array($headerArray['Content-Length']) && count($headerArray['Content-Length']) == 2) {
					$header['size'] = $headerArray['Content-Length'][1];
				} else {
					$header['size'] = $headerArray['Content-Length'] ?? 0;
				}
			}
			if (!isset($headerArray['Content-Type'])) {
				$header['type'] = 'image/jpeg';
			} else {
				if (is_array($headerArray['Content-Type']) && count($headerArray['Content-Type']) == 2) {
					$header['type'] = $headerArray['Content-Type'][1];
				} else {
					$header['type'] = $headerArray['Content-Type'] ?? 'image/jpeg';
				}
			}
		} catch (\Exception $e) {
		}
		return $header;
	}
}

if (!function_exists('formatFileSize')) {

	/**
	 * 格式化文件大小
	 * @param $size
	 * @return mixed|string|null
	 */
	function formatFileSize($size)
	{
		if (!$size) {
			return '0KB';
		}
		try {
			$toKb = 1024;
			$toMb = $toKb * 1024;
			$toGb = $toMb * 1024;
			if ($size >= $toGb) {
				return round($size / $toGb, 2) . 'GB';
			} elseif ($size >= $toMb) {
				return round($size / $toMb, 2) . 'MB';
			} elseif ($size >= $toKb) {
				return round($size / $toKb, 2) . 'KB';
			} else {
				return $size . 'B';
			}
		} catch (\Exception $e) {
			return '0KB';
		}
	}

}


if (!function_exists('checkCoordinates')) {
	/**
	 * 检测经纬度数据
	 * @param $longitude
	 * @param $latitude
	 * @return bool
	 */
	function checkCoordinates($longitude, $latitude)
	{
		if ($longitude) {
			$longitudePattern = '/^(-?\d{1,3}(?:\.\d+)?)$/'; // 经度，允许1到3位整数，后面跟着小数
			if (!preg_match($longitudePattern, $longitude)) {
				return false; // 经度格式不正确
			}
			// 检查经纬度是否在有效范围内
			if (($longitude < -180) || ($longitude > 180)) {
				return false; // 经度超出范围
			}
		}
		if ($latitude) {
			$latitudePattern = '/^[-+]?([0-8]?\d(\.\d+)?|90(\.0+)?)$/'; // 纬度，允许-90到90，包括小数部分
			if (!preg_match($latitudePattern, $latitude)) {
				return false; // 纬度格式不正确
			}
			if (($latitude < -90) || ($latitude > 90)) {
				return false; // 纬度超出范围
			}
		}
		// 如果所有检查都通过，则返回true
		return true;
	}
}

if (!function_exists('filter_str')) {
	/**
	 * 过滤字符串敏感字符
	 * @param $str
	 * @return array|mixed|string|string[]|null
	 * @throws Exception
	 */
	function filter_str($str)
	{
		$param_filter_data = sys_config('param_filter_data');
		$param_filter_type = sys_config('param_filter_type', 3);
		$rules = preg_split('/\r\n|\r|\n/', base64_decode($param_filter_data));
		if ($param_filter_data) {
			switch ($param_filter_type) {
				case 1://防火墙关闭
					return $str;
				case 2://报错
					foreach ($rules as $rule) {
						if (preg_match($rule, $str)) {
							throw new \Exception('您的参数存在非要请求,已被拦截');
						}
					}
					break;
				case 3://过滤
					if (filter_var($str, FILTER_VALIDATE_URL)) {
						$url = parse_url($str);
						if (!isset($url['scheme'])) return $str;
						$host = $url['scheme'] . '://' . $url['host'];
						$str = $host . preg_replace($rules, '', str_replace($host, '', $str));
					} else {
						$str = preg_replace($rules, '', $str);
					}
					return $str;
			}
		}
		return $str;
	}
}


